//
//  YJSeekViewController.m
//  Healthfood
//
//  Created by qianfeng01 on 15/7/9.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJSeekViewController.h"
#import "YJHealthModel.h"
#import "AFNetworking.h"
#import "YJinformationViewController.h"
#import "UIImageView+WebCache.h"
#import "YJDetadetaViewController.h"
#import "YJDetaModel.h"
#import "YJMenuModel.h"


@interface YJSeekViewController ()
{
    
    UITableView *_tabaView;
    NSMutableArray *_dataArray;
    AFHTTPRequestOperationManager *_manager;
    UIImageView *_imageView;
    UILabel *_label;
    UILabel *_Taillabel;
}
@property (nonatomic,strong)YJMenuModel *menuModel;
@property (nonatomic,strong)YJDetaModel *detaModel;
@property (nonatomic,strong)UILabel *Taillabel;
@property (nonatomic,strong)UILabel *label;
@property(nonatomic,strong)YJHealthModel *model;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;


@end

@implementation YJSeekViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self menuloadData];
    [self loadData];
}
-(void)menuloadData{
    
    self.manager=[AFHTTPRequestOperationManager manager];
    self.manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    NSString *url =[NSString stringWithFormat:kUrlmenu,(long)self.currentPage];
      __weak typeof (self)weakSelf =self;
    [self.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *menuDict =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSArray *arry= menuDict[@"data"];
            int a=arc4random()%arry.count-1;
           
            NSDictionary *dict=arry[a];
                _menuModel =[[YJMenuModel alloc] init];
                _menuModel.name =dict[@"name"];;
                _menuModel.imgs =dict[@"imgs"];
                _menuModel.code =[dict[@"code"] integerValue];
                weakSelf.code=_menuModel.code;
                [weakSelf detaloadData];
    
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


-(void)detaloadData{

    NSString *url =[NSString stringWithFormat:kUrlDeta,(long)self.code];
    [self.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary * detaDict =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dicts =detaDict[@"data"];
            NSArray *array = dicts[@"dishs"];
            
            for (NSDictionary *dict in array) {
            
                _detaModel =[[YJDetaModel alloc] init];
                _detaModel.dishsName =dict[@"name"];
                _detaModel.image =dict[@"img"];
                _detaModel.code = dict[@"code"];
               
                [self.dataArray addObject:_detaModel];
            }
            [self.tableView reloadData];
            
            [self addView];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}
-(void)loadData{
    self.manager=[AFHTTPRequestOperationManager manager];
    self.manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    int b =arc4random()%9;
    NSString *url =[NSString stringWithFormat:kUrl,(long)b];
    [self.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *ledgeDict =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dicts =ledgeDict[@"data"];
            NSArray *array =dicts[@"nous"];
            for (NSDictionary *dict in array) {
            _model =[[YJHealthModel alloc] init];
            [_model setValuesForKeysWithDictionary:dict];

            [self.dataArray addObject:_model];
                
            }
            [self.tableView reloadData];
            [self addView];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(void)addView{
    UIView *view =[[UIView alloc] initWithFrame:self.view.bounds];
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame=CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height/2.5+30);
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(5, 30, self.view.frame.size.width-10, self.view.frame.size.height/2.5)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:_model.img] placeholderImage:[UIImage imageNamed:@"food8"]];
    [button addSubview:imgView];
    [button addTarget:self action:@selector(bouttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    self.view.hidden =NO;
    
    _label=[[UILabel alloc] initWithFrame:CGRectMake(5,60, self.view.frame.size.width-10, 30)];
    _label.backgroundColor =[UIColor grayColor];
    _label.alpha=0.5;
    _label.text = [NSString stringWithFormat:@"精品推荐:%@",_model.title];
    _label.textColor =[UIColor greenColor];
    UIButton *Tailbutton =[UIButton buttonWithType:(UIButtonTypeSystem)];
    Tailbutton.frame =CGRectMake(0, button.frame.size.height+10, _label.frame.size.width, button.frame.size.height+10);
    UIImageView *TailimgView=[[UIImageView alloc] initWithFrame:CGRectMake(5, 30, self.view.frame.size.width-10, self.view.frame.size.height/2.5+25)];
    [TailimgView sd_setImageWithURL:[NSURL URLWithString:_detaModel.image] placeholderImage:[UIImage imageNamed:@"food9"]];
    [Tailbutton addSubview:TailimgView];
    _Taillabel =[[UILabel alloc] initWithFrame:CGRectMake(5, button.frame.size.height+40, self.view.frame.size.width-10, 40)];
    _Taillabel.text =[NSString stringWithFormat:@"精品推荐:%@",_detaModel.dishsName];
    _Taillabel.backgroundColor =[UIColor grayColor];
    _Taillabel.alpha =0.5;
    _Taillabel.textColor =[UIColor greenColor];
    
    
    
    [Tailbutton addTarget:self action:@selector(TailbuttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:button];
    [view addSubview:_label];
    [view addSubview:Tailbutton];
    [view addSubview:_Taillabel];
    
    self.view =view;
}
-(void)TailbuttonClick:(UIButton *)button{
    YJDetadetaViewController *controller =[[YJDetadetaViewController alloc] init];
    controller.code =_detaModel.code;
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)bouttonClick:(UIButton*)button{
    YJinformationViewController *controller =[[YJinformationViewController alloc] init];
    controller.code =_model.code;
    
    [self.navigationController pushViewController:controller animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!tableView.editing)
        return UITableViewCellEditingStyleNone;
    else {
        return UITableViewCellEditingStyleDelete;
    }
    
}

@end
